package cn.b.sky.message.service.impl;

import cn.b.sky.common.exception.InfoException;
import cn.b.sky.message.dao.MessageDao;
import cn.b.sky.message.model.InstantMessageInfo;
import cn.b.sky.message.service.MessageSenderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class InstantMessageSenderImpl implements MessageSenderService {

  private static final Logger LOGGER = LoggerFactory.getLogger(InstantMessageSenderImpl.class);

  @Resource
  private MessageDao messageDao;

  /**
   * 发送站内信消息
   * 
   * @param message
   * @param userCode
   */
  @Override
  public void sentMessage(String messageCode, String message, String siteCode, String userCode) {
    try {
      InstantMessageInfo instantMessage = new InstantMessageInfo();
      instantMessage.setSiteCode(siteCode);
      instantMessage.setMessageContent(message);
      instantMessage.setMessageCode(messageCode);
      instantMessage.setReadFlag("0");
      instantMessage.setUserCode(userCode);
      instantMessage.setReceivedTime(new Date());
      messageDao.insertInstantMessage(instantMessage);
    } catch (Exception e) {
      LOGGER.error("{}",e);
      throw new InfoException(e.getMessage());
    }

  }

}
